package org.bongo.open.cumulo.procengine.runner;

import org.bongo.open.cumulo.core.context.CumuloContext;
import org.bongo.open.cumulo.core.utils.PoolableObjectFactory;
import org.bongo.open.cumulo.procengine.broker.exceptions.ProcRunnerException;

public class SimpleProcRunnerFactory implements PoolableObjectFactory<ProcRunner> {

	private CumuloContext context;
	private static int identifier  = 0;
	
	public SimpleProcRunnerFactory(CumuloContext c) {
		context = c;
	}
	
	public ProcRunner createObject() {
		ProcRunnerContainer container = new SimpleJvmContainer();
		try {
			container.setIdentifier(identifier++);
			container.startup(context);
		} catch (ProcRunnerException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		SimpleProcRunner runner = new SimpleProcRunner(container);
		
		return runner;
	}

	public void cleanup(ProcRunner runner) {
		try {
			runner.getContainer().cleanup(context);
		} catch (ProcRunnerException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	}

	
}
